package com.cloud.wms.ui.group.obtain;


import android.content.Context;
import android.content.Intent;
import android.os.Message;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.cloud.core.utils.TimeUtils;
import com.cloud.core.widget.decoration.GridItemDecoration;
import com.cloud.widget.ScanText;
import com.cloud.widget.utils.DensityUtils;
import com.cloud.wms.R;
import com.cloud.wms.base.BaseBarListActivity;
import com.cloud.wms.config.IntentKey;
import com.cloud.wms.ui.group.contract.GroupObtainContract;
import com.cloud.wms.ui.group.entity.ObtainScanContainerEntity;
import com.cloud.wms.ui.group.presenter.GroupObtainPresenter;
import com.cloud.wms.ui.main.entity.TaskEntity;

import butterknife.BindView;

/**
 * Created by FC on 2021/4/14.
 * Desc：团购领用任务-扫描容器
 */
public class ObtainScanContainerActivity extends BaseBarListActivity<ObtainScanContainerEntity.ContainerEntity, GroupObtainPresenter> implements GroupObtainContract.View {

    @BindView(R.id.scan_text)
    ScanText scanText;
    @BindView(R.id.tv_jh_code)
    TextView tvJhCode;
    private long batchId;
    private TaskEntity taskEntity;

    @Override
    protected String barTitle() {
        return "团购领用";
    }

    @Override
    protected int getResId() {
        return R.layout.activity_obtain_scan_container;
    }

    @Override
    protected int getItemResId() {
        return R.layout.item_obtain_scan_container;
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        initScanText(scanText);
        batchId = getIntent().getLongExtra(IntentKey.ID_KEY, 0);
        taskEntity = getIntent().getParcelableExtra(IntentKey.TASK_KEY);
        if (taskEntity != null)
            taskEntity.setBeginTime(TimeUtils.getCurrentDateAndTime());
        mPresenter.getBatchContainerMsg(taskEntity == null ? batchId : taskEntity.getBillID());
    }

    @Override
    public void onReload() {
        super.onReload();
        mPresenter.getBatchContainerMsg(taskEntity == null ? batchId : taskEntity.getBillID());
    }

    @Override
    protected void onScanHandle(String result, int resultCode) {
        super.onScanHandle(result, resultCode);
        scanText.addHistory(result);
        int scanAmount = 0;
        boolean isConfirm = false;
        for (int i = 0; i < mAdapter.getData().size(); i++) {
            ObtainScanContainerEntity.ContainerEntity containerEntity = mAdapter.getItem(i);
            if (result.equalsIgnoreCase(containerEntity.getContainerCode())) {
                containerEntity.setScan(true);
                mAdapter.notifyDataSetChanged();
                isConfirm = true;
            }
            if (containerEntity.isScan()) {
                scanAmount++;
                if (scanAmount == mAdapter.getData().size()) {
                    ObtainInventoryActivity.start(this, taskEntity, batchId);
                    finish();
                }
            }
        }
        if (!isConfirm) {
            info("容器扫描错误");
        }
    }

    @Override
    protected RecyclerView.LayoutManager getLayoutManager() {
        return new GridLayoutManager(this, 3);
    }

    @Override
    protected RecyclerView.ItemDecoration itemDecoration() {
        return GridItemDecoration.newBuilder().spanCount(3)
                .verticalDivider(ContextCompat.getDrawable(this, R.color.bg_base_content), DensityUtils.Dp2Px(this, 6), false)
                .horizontalDivider(ContextCompat.getDrawable(this, R.color.bg_base_content), DensityUtils.Dp2Px(this, 6), false)
                .build();
    }

    @Override
    protected void bindItemData(BaseViewHolder helper, ObtainScanContainerEntity.ContainerEntity item) {
        helper.setText(R.id.tv_container_code, "容器：" + item.getContainerCode())
                .setVisible(R.id.label_view, item.isScan());
    }

    @Override
    protected void setupComponent() {
        getActivityComponent().inject(this);
    }

    @Override
    public void onSuccess(Message message) {
        super.onSuccess(message);
        switch (message.what) {
            case GroupObtainPresenter.CONTAINER_MSG:
                ObtainScanContainerEntity entity = (ObtainScanContainerEntity) message.obj;
                tvJhCode.setText("集货地：" + entity.getJHContainerCode());
                mAdapter.setNewInstance(entity.getLstContainerInfo());
                break;
        }
    }

    public static void start(Context context, TaskEntity taskEntity, long billId) {
        Intent intent = new Intent(context, ObtainScanContainerActivity.class);
        intent.putExtra(IntentKey.ID_KEY, billId);
        intent.putExtra(IntentKey.TASK_KEY, taskEntity);
        context.startActivity(intent);
    }
}
